import java.lang.reflect.Array;
import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:张熳
 * Date:2025-10-18
 * Time:22:28
 */

//使用顺序表的思路模拟实现 栈Stack
public class MyStack implements IStack{
    public int[] array;
    public int usedSize;

    public MyStack() {
        this.array = new int[10];
    }

    //判断栈是否已经满了
    public boolean isFull() {
        return this.usedSize == array.length;
    }
    @Override
    public int push(int val) {
        if (isFull()) {
            //如果满了，扩容
            this.array = Arrays.copyOf(this.array,2*this.array.length);
        }
        array[usedSize++] = val;
        return val;
    }


    @Override
    /*public int pop() {
        try {
            if (empty()) {
                throw new EmptyStackException("空栈");
            }
            int val = array[usedSize-1];
            usedSize--;
            return val;
        }catch (EmptyStackException e) {
            e.printStackTrace();
            return -1;
        }
    }*/
    public int pop() {
        if (empty()) {
            throw new EmptyStackException("空栈");
        }
        int val = array[usedSize-1];
        usedSize--;
        return val;
    }

    @Override
    public boolean empty() {
        return usedSize == 0;
    }

    @Override
    public int peek() {
        if (empty()) {
            throw new EmptyStackException("空栈");
        }
        return array[usedSize-1];
    }

    @Override
    public int size() {
        return this.usedSize;
    }
}


